<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link media="all" rel="stylesheet" th:href="@{/back/lib/layui-v2.6.3/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/back/css/public.css}">
    <!--    <title>富文本编辑器5</title>-->
    <link rel="stylesheet" th:href="@{/back/lib/wangEditor5/css/style.css}" media="all">

    <style>
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100; /* 按需定义 */
        }

        #toolbar-container {
            border-bottom: 1px solid #ccc;
        }

        #editor-container {
            height: 500px;
        }
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <!--    修改使用的用户ID-->
    <input hidden="hidden" name="id" th:value="${article.id}">
    <div class="layui-form-item">
        <label class="layui-form-label required">文章作者ID</label>
        <div class="layui-input-block">
            <input class="layui-input" name="articleUsernameId" placeholder="请输入文章作者ID"
                   th:value="${article.articleUsernameId}" type="text"
                   lay-verify="required" disabled="disabled" style="background:#CCCCCC">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章标题</label>
        <div class="layui-input-block">
            <input class="layui-input" name="title" placeholder="文章标题" th:value="${article.title}" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章内容</label>
        <div class="layui-input-block">
            <div id="editor—wrapper">
                <div id="toolbar-container"><!-- 工具栏 --></div>
                <input class="layui-input" name="content" id="content" placeholder="请输入文章内容"
                       th:value="${article.content}" type="text"
                       disabled="disabled" style="background:#CCCCCC">
                <div id="editor-container" class="layui-input"></div><!-- 编辑器 -->
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章分类</label>
        <div class="layui-input-block">
            <input class="layui-input" name="categories" placeholder="请输文章分类" th:value="${article.categories}"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章标签</label>
        <div class="layui-input-block">
            <input class="layui-input" name="tags" placeholder="请输入文章标签" th:value="${article.tags}" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否允许评论</label>
        <div class="layui-input-block">
            <input class="layui-input" name="allowComment" placeholder="是否允许评论" th:value="${article.allowComment}"
                   type="text">
        </div>
    </div>
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">文章缩略图</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input class="layui-input" name="thumbnail" placeholder="请输入文章缩略图" th:value="${article.thumbnail}"-->
    <!--                   type="text">-->
    <!--        </div>-->
    <!--    </div>-->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
        </div>
    </div>
</div>

<script charset="utf-8" th:src="@{/back/lib/layui-v2.6.3/layui.js}"></script>
<!--富文本编辑器5-->
<script th:src="@{/back/lib/wangEditor5/js/index.js}" charset="utf-8"></script>
<!--，是Thymeleaf中的内联写法，支持在JS访问model中的数据。用两个中括号就能像在html中使用对象了。-->
<script th:inline="javascript">

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        <!--富文本编辑器5-->
        const {createEditor, createToolbar} = window.wangEditor
        const editorConfig = {
            placeholder: '请输入内容...',
            onChange(editor) {
                const html = editor.getHtml()
                console.log('editor content', html)
                // 也可以同步到 <textarea>
                $('#content').val(html)
            }
        }
        const editor = createEditor({
            selector: '#editor-container',
            html: `<div>[[${article.content}]]</div>`,
            config: editorConfig,
            mode: 'default', // 'default' or 'simple'
        })

        const toolbarConfig = {}
        const toolbar = createToolbar({
            editor,
            selector: '#toolbar-container',
            config: toolbarConfig,
            mode: 'default', // or 'simple'
        })
        //=================================================
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {
                $.ajax("/back-article/update", {
                    data: data.field,
                    type: "POST",
                    dataType: "JSON",
                    success: function (result) {
                        console.log(data)
                        if (result.code == 0) {
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            window.parent.location.reload();//父窗口刷新
                        }
                    },
                })
            });
            return false;
        });
    });
</script>
</body>
</html>